package com.lam.mall.service.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.components.redis.RedisUtil;
import com.lam.common.exception.BusinessException;
import com.lam.common.utils.StringUtils;
import com.lam.mall.app.param.UserRegisterParam;
import com.lam.mall.entity.MallUser;
import com.lam.mall.mapper.MallUserMapper;
import com.lam.mall.param.MallUserParam;
import com.lam.mall.service.MallUserService;

import cn.hutool.core.util.StrUtil;

/**
 * 商城用户Service业务层处理
 * 
 * @author admin
 * @date 2023-04-20
 */
@Service
public class MallUserServiceImpl  implements MallUserService {

    @Autowired
    private MallUserMapper mallUserMapper;

    /**
     * 查询商城用户
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public MallUser selectById(String id) {

        return mallUserMapper.selectById(id);
    }
    
    @Override
    @Transactional(readOnly = true)
    public MallUser selectByUsername(String username) {
    	QueryWrapper<MallUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
    	return mallUserMapper.selectOne(queryWrapper);
    }
    
    @Override
    @Transactional(readOnly = true)
    public MallUser selectByPhone(String phone) {
    	QueryWrapper<MallUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
    	return mallUserMapper.selectOne(queryWrapper);
    }
    
    @Override
    @Cacheable(cacheNames = "mall_user", key = "#userId")
    public MallUser getCahceByUserId(String userId) {
        return mallUserMapper.selectById(userId);
    }
    
    @Override
    @CacheEvict(cacheNames = "mall_user", key = "#userId")
    public void removeCahceByUserId(String userId) {
    }
    
    /**
     * 查询商城用户列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<MallUser> selectList(MallUserParam param) {
    
        QueryWrapper<MallUser> queryWrapper = new QueryWrapper<>();

        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
            queryWrapper.and(wr->{
                wr.or().like("user_code", param.getKeywords());
                wr.or().like("nick_name", param.getKeywords());
                wr.or().like("realname", param.getKeywords());
                wr.or().like("username", param.getKeywords());
                wr.or().like("phone", param.getKeywords());
                wr.or().like("email", param.getKeywords());
            });
        }
        
        queryWrapper.like(StringUtils.isNotBlank(param.getUserCodeKey()), "user_code", param.getUserCodeKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getUserCode()), "user_code", param.getUserCode());
        queryWrapper.like(StringUtils.isNotBlank(param.getNickNameKey()), "nick_name", param.getNickNameKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getNickName()), "nick_name", param.getNickName());
        queryWrapper.like(StringUtils.isNotBlank(param.getRealnameKey()), "realname", param.getRealnameKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getRealname()), "realname", param.getRealname());
        queryWrapper.like(StringUtils.isNotBlank(param.getUsernameKey()), "username", param.getUsernameKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getUsername()), "username", param.getUsername());
        queryWrapper.eq(Objects.nonNull(param.getStatus()), "status", param.getStatus());
        queryWrapper.like(StringUtils.isNotBlank(param.getPhoneKey()), "phone", param.getPhoneKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getPhone()), "phone", param.getPhone());
        queryWrapper.eq(Objects.nonNull(param.getSex()), "sex", param.getSex());
        queryWrapper.like(StringUtils.isNotBlank(param.getEmailKey()), "email", param.getEmailKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getEmail()), "email", param.getEmail());

        return mallUserMapper.selectList(queryWrapper);
    }

    /**
     * 新增商城用户
     * 
     * @param mallUser 
     * @return 
     */
    @Override
    @Transactional
    public int insert(MallUser mallUser) {
    
        return mallUserMapper.insert(mallUser);
    }

    /**
     * 修改商城用户
     * 
     * @param mallUser 
     * @return 
     */
    @Override
    @Transactional
    public int update(MallUser mallUser) {
    
        return mallUserMapper.updateById(mallUser);
    }

    /**
     * 删除商城用户信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    
        if(StringUtils.isBlank(idStr)) {
            return 0;
        }
        return mallUserMapper.deleteBatchIds(StringUtils.str2List(idStr));
    }
    
    /**
     * 看看有没有校验验证码成功的标识
     * @param userRegisterParam
     * @param checkRegisterSmsFlag
     */
    @Override
    public void validate(UserRegisterParam userRegisterParam, String checkRegisterSmsFlag) {
        if (StrUtil.isBlank(userRegisterParam.getCheckRegisterSmsFlag())) {
            // 验证码已过期，请重新发送验证码校验
            throw new BusinessException("验证码已过期，请重新发送验证码校验");
        } else {
            String checkRegisterSmsFlagMobile = (String) RedisUtil.get(checkRegisterSmsFlag);
            if (!Objects.equals(checkRegisterSmsFlagMobile, userRegisterParam.getMobile())) {
                // 验证码已过期，请重新发送验证码校验
                throw new BusinessException("验证码已过期，请重新发送验证码校验");
            }
        }
    }

    
}
